package com.adnaan.restaurant.controller;

import com.adnaan.restaurant.common.exception.BizException;
import com.adnaan.restaurant.common.untils.R;
import com.adnaan.restaurant.entity.ResShoppingCart;
import com.adnaan.restaurant.service.IResShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: adnaan
 * create: 2023-12-09 17:15
 * Description:
 */
@Tag(name = "购物车接口", description = "购物车相关功能")
@RestController
@RequestMapping("/restaurant/shopping-cart")
public class ResShoppingCartController {

    @Autowired
    private IResShoppingCartService resShoppingCartService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "添加购物车")
    @PostMapping("/add")
    public R add(@RequestBody ResShoppingCart resShoppingCart) {
        ResShoppingCart existingCart = resShoppingCartService.selectShoppingCartByDishesId(resShoppingCart.getUserId(), resShoppingCart.getDishesId());
        int updatedRows;
        if (existingCart != null) {
            Integer newQuantity = existingCart.getQuantity() + resShoppingCart.getQuantity();
            updatedRows = resShoppingCartService.updateShoppingCartNum(existingCart.getId(), newQuantity);
        } else {
            updatedRows = resShoppingCartService.insertShoppingCart(resShoppingCart);
        }
        if (updatedRows > 0) {
            return R.success("添加成功");
        } else {
            return R.error(500, "添加失败");
        }
    }

    @Operation(summary = "购物车列表")
    @GetMapping("/findAll")
    public R findAll(@RequestParam Long userId, @RequestParam Long shopId) {
        List<ResShoppingCart> list = resShoppingCartService.findAll(userId, shopId);
        return R.success(list);
    }

    @Operation(summary = "移除购物车的商品")
    @DeleteMapping("/del")
    public R delCartDishes(@RequestParam Long id) {
        int deleteStatus = resShoppingCartService.delCartDishes(id);
        if (deleteStatus > 0) {
            return R.success("删除成功");
        } else {
            return R.error(500, "删除失败");
        }
    }

    @Operation(summary = "清空购物车")
    @DeleteMapping("/clear")
    public R clearCart(@RequestParam Long userId, @RequestParam Long shopId) {
        int clearCart = resShoppingCartService.clearCart(userId, shopId);
        if (clearCart > 0) {
            return R.success("删除成功");
        } else {
            return R.error(500, "删除失败");
        }
    }

    @Operation(summary = "更新购买菜品数量")
    @PutMapping("/updateNum")
    public R updateNum(@RequestParam Long id, @RequestParam Integer newQuantity) {
        if (newQuantity <= 0) {
            throw new BizException(403, "数量不能小于等于0");
        }
        int saveStatus = resShoppingCartService.updateNum(id, newQuantity);
        if (saveStatus > 0) {
            return R.success("修改成功");
        } else {
            return R.error(500, "修改失败");
        }
    }
}
